package com.culturesalon.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.culturesalon.connectionfactory.ConnectionFactory;
import com.culturesalon.dto.NoticeCategory;

public class NoticeCategoryDAOImpl implements NoticeCategoryDAO {

	@Override
	public NoticeCategory get(Integer id) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			conn=ConnectionFactory.getConnection();
			StringBuffer sql=new StringBuffer();
			sql.append(" SELECT ID,CATEGORYNAME,CATEGORYDES FROM T_NOTICECATEGORY ");
			sql.append(" WHERE ID=? ");
			pstm=conn.prepareStatement(sql.toString());
			pstm.setObject(1, id);
			rs=pstm.executeQuery();
			NoticeCategory noticeCategory=null;
			if(rs.next()){
				noticeCategory=new NoticeCategory();
				noticeCategory.setId(rs.getInt("id"));
				noticeCategory.setCategoryName(rs.getString("categoryName"));
				noticeCategory.setCategoryDes(rs.getString("categoryDes"));
			}
			return noticeCategory;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn, rs, pstm);
		}
	}

	@Override
	public List<NoticeCategory> findAll() {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			conn=ConnectionFactory.getConnection();
			List<NoticeCategory> noticeCategories=new ArrayList<NoticeCategory>();
			StringBuffer sql=new StringBuffer();
			sql.append(" SELECT  ID,CATEGORYNAME,CATEGORYDES FROM T_NOTICECATEGORY ");
			pstm=conn.prepareStatement(sql.toString());
			rs=pstm.executeQuery();
			NoticeCategory noticeCategory=null;
			while(rs.next()){
				noticeCategory=new NoticeCategory();
				noticeCategory.setId(rs.getInt("id"));
				noticeCategory.setCategoryName(rs.getString("categoryName"));
				noticeCategory.setCategoryDes(rs.getString("categoryDes"));
				noticeCategories.add(noticeCategory);
			}
			return noticeCategories;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn, rs, pstm);
		}
	}

}
